package com.gzw.shop.fragment;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cjj.MaterialRefreshLayout;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.google.gson.reflect.TypeToken;
import com.gzw.shop.Contans;
import com.gzw.shop.R;
import com.gzw.shop.activity.WareDetailActivity;
import com.gzw.shop.adapter.BaseAdapter;
import com.gzw.shop.adapter.CategoryAdapter;
import com.gzw.shop.adapter.DividerGridItemDecoration;
import com.gzw.shop.adapter.DividerItemDecoration;
import com.gzw.shop.adapter.WaresAdapter;
import com.gzw.shop.bean.Banner;
import com.gzw.shop.bean.Category;
import com.gzw.shop.bean.Page;
import com.gzw.shop.bean.Wares;
import com.gzw.shop.http.OkHttpHelper;
import com.gzw.shop.http.SpotsCallBack;
import com.gzw.shop.utils.Pager;
import com.gzw.shop.widget.GToolbar;

import java.util.List;

import butterknife.BindView;
import okhttp3.Response;


public class CategoryFragment extends BaseFragment {

    @BindView(R.id.toolbar)
    GToolbar toolbar;
    @BindView(R.id.rv_one)
    RecyclerView rv_one;
    @BindView(R.id.slider)
    SliderLayout mSliderLayout;
    @BindView(R.id.rv_two)
    RecyclerView rv_two;
    @BindView(R.id.refresh)
    MaterialRefreshLayout mRefreshLayout;

    private int currPage = 1;
    private int pageSize = 10;
    private int totalPage = 0;
    private long category_id = 0;

    private static final int STATE_NORMAL = 0;//正常
    private static final int STATE_REFRESH = 1;//刷新
    private static final int STATE_MORE = 2;//加载更多
    private int state = STATE_NORMAL;

    private CategoryAdapter mAdapter;
    private WaresAdapter wersAdapter;

//    private List<Category> mCategories = new ArrayList<>();

    private OkHttpHelper mHelper = OkHttpHelper.getInstance();

    private Pager mPager;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_category;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

        mPager = Pager.newBuilder().setCanLoadMore(true)
                .setUrl(Contans.API.WARES_LIST)
//                .putParam("categoryId",category_id)
                .setRefreshLayout(mRefreshLayout)
                .setPageSize(10)
                .setOnPagerListener(new Pager.OnPagerListener() {
                    @Override
                    public void load(final List datas, int totalPage, int totalCount) {

                        if (wersAdapter == null) {
                            wersAdapter = new WaresAdapter(getActivity(), datas);
                            rv_two.setAdapter(wersAdapter);
                            wersAdapter.setListener(new BaseAdapter.OnItemClickListener() {
                                @Override
                                public void OnClick(View view, int position) {

                                    Intent intent = new Intent(getActivity(), WareDetailActivity.class);
                                    intent.putExtra(Contans.WARES, (Wares) datas.get(position));
                                    startActivity(intent);
                                }
                            });
                            rv_two.setLayoutManager(new GridLayoutManager(getActivity(), 2));
                            rv_two.setItemAnimator(new DefaultItemAnimator());
                            rv_two.addItemDecoration(new DividerGridItemDecoration(getActivity()));
                        } else {
                            wersAdapter.clearData();
                            wersAdapter.addData(datas);
                            rv_two.scrollToPosition(0);
                        }

                    }

                    @Override
                    public void refresh(List datas, int totalPage, int totalCount) {
                        wersAdapter.clearData();
                        wersAdapter.addData(datas);
                        rv_two.scrollToPosition(0);
                    }

                    @Override
                    public void loadMore(List datas, int totalPage, int totalCount) {
                        wersAdapter.addData(mAdapter.getDatas().size(), datas);
                        rv_two.scrollToPosition(mAdapter.getDatas().size());
                    }
                }).builder(getActivity(), new TypeToken<Page<Wares>>() {
                }.getType());

        requestCategoryData();
        requestBannerData();

    }

    //请求轮播图数据
    private void requestBannerData() {
        String url = Contans.API.BANNER + "?type=1";
        mHelper.get(url, new SpotsCallBack<List<Banner>>(getActivity()) {
            @Override
            public void onSuccess(Response response, List<Banner> banners) {

                showBanners(banners);
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });
    }

    private void showBanners(List<Banner> banners) {
        if (banners != null) {

            for (int i = 0; i < banners.size(); i++) {
                TextSliderView textSliderView = new TextSliderView(getActivity());
                textSliderView
                        .image(banners.get(i).getImgUrl())
                        .description(banners.get(i).getName())
                        .setScaleType(BaseSliderView.ScaleType.Fit);
                mSliderLayout.addSlider(textSliderView);
            }
        }
        mSliderLayout.setPresetTransformer(SliderLayout.Transformer.RotateUp);
        mSliderLayout.setDuration(4000);
    }

    private void requestCategoryData() {

        mHelper.get(Contans.API.CATEGORY_LIST, new SpotsCallBack<List<Category>>(getActivity()) {
            @Override
            public void onSuccess(Response response, List<Category> categories) {
//                mCategories = categories;
                showData(categories);

                if (categories != null && categories.size() != 0) {
                    category_id = categories.get(0).getId();
                    mPager.putParam("categoryId", category_id);
                    mPager.request();
//                    requestWares(category_id);
                }
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });

    }

    private void showData(List<Category> categories) {

        mAdapter = new CategoryAdapter(getActivity(), categories);

        mAdapter.setListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void OnClick(View view, int position) {
                Category category = mAdapter.getItem(position);

                currPage = 1;
                state = STATE_NORMAL;
                category_id = category.getId();
//                requestWares(category_id);
                mPager.putParam("categoryId", category_id);
                mPager.request();
            }
        });

        rv_one.setAdapter(mAdapter);
        rv_one.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_one.setItemAnimator(new DefaultItemAnimator());
        rv_one.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
    }

    public void refreshView() {
        if (mAdapter == null) {
            initData();
        }
    }
}
